var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submit:0,
    isShowTime:false,
    isShowTime2:false,
    changeReason:"",
    deptName:'',
    deptId:'',
    teamName:'',
    teamId:'',
    newTeamMembers:'',
    newMembersName:'',
    newWorkLeaderId:'',
    newWorkLeaderName:'',
    leaveTime:'',
    addTime:'',
    dateNum:'',
    ticketInfo:'',
    ticketNumber:'',
    signImg:'',
    type:''
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var form = Store.getState().formInfor.form
    var key = form.key
    console.log(form)
    this.setData({
      signImg: wx.getStorageSync('signUrl'),
    })
    switch (key) {
      case 'workSign': // 获取签发人签名  
        this.setData({
          signImg: form.selected,
        })
        break 
      case 'selectWorkTeam': // 获取班组  
        this.setData({
          "teamName": form.selected.teamName,
          "teamId": form.selected.teamId,
          "newMembersName": '',
          "newTeamMembers": '',
        })
        break
      case 'selectWorkPeops':
        this.setData({
          "newMembersName": form.selected.map((item)=>item.userName),
          "newTeamMembers":form.selected.map((item)=>item.userId),
        })
        break
      case 'selectWorkLeader':
        this.setData({
          "newWorkLeaderName": form.selected.userName,
          "newWorkLeaderId":form.selected.userId,
        })
        break
      default:
        break;
    }
    console.log(this.data.signImg)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      dateNum:new Date().getTime()
    })
    if ('ticketInfo' in options){ 
      this.setData({
        ticketNumber:JSON.parse(options.ticketInfo).ticketNumber, 
        ticketInfo:JSON.parse(options.ticketInfo),
        type:options.type,
      })
    }
  },
  beginSign(e){  
    var that=this
    if(!Boolean(that.data.signImg)){
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'workSign', 
        },
      })
      $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    }
    // wx.showModal({
    //   title: '提示',
    //   content: '使用原有签名或者手动签名',
    //   confirmText:'原有签名',
    //   cancelText:'手动签名',
    //   success (res) {
    //     if (res.confirm) {
          
    //       that.setData({
    //         signImg: wx.getStorageSync('signUrl'),
    //       })
    //     } else if (res.cancel) {
    //       Store.dispatch({
    //         type: 'SET_FROM',
    //         form: {
    //           key: 'workSign', 
    //         },
    //       })
    //       $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
    //     }
    //   }
    // })
  },
  bigSign(e){
    const {img}=$.detail(e)
    wx.previewImage({
      current: '', // 当前显示图片的链接，不填则默认为urls的第一张
      urls: [img] // 需要预览的图片链接列表
    })
  },
  selectDepartment() {
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectTickDpt', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)  
  },
  selectWorkTeam() { // 选班组
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectWorkTeam', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
          areaId:''
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectWorkPeops() { // 选工作人员
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectWorkPeops', // 后台要的更改字段 
        type:2, // 表单类型 1： radio  2：select
        config: {
          dataType:'object',
          teamId: this.data.teamId ,
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectWorkLeader(){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectWorkLeader', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
          deptId:0
          // deptId: this.data.ticketInfo.deptId
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  input_handle(e){
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  cancelBack(){
    $.linkurl('','back')
  },
  subMember(){
    if(this.data.type=='更换班组人员'){
      if(this.data.newTeamMembers==''||!Boolean(this.data.signImg)){
        $.toast('请选择新班组人员并签字确认')
        return false
      }
      api.$get(api.workTicket.teamMemberChange, {
        ticketNumber:this.data.ticketNumber,
        newTeamMembers:this.data.newTeamMembers.join(','),
        changeReason:this.data.changeReason,
        signature:this.data.signImg,
      }).then((res) => {
        $.linkurl('','back')
      })
    }
    if(this.data.type=='变更负责人'){
      if(this.data.newWorkLeaderId==''){
        $.toast('请选择新的工作负责人')
        return false
      }
      api.$get(api.workTicket.workLeaderChange, {
        ticketNumber:this.data.ticketNumber,
        newWorkLeaderId:this.data.newWorkLeaderId,
        changeReason:this.data.changeReason,
      }).then((res) => {
        $.linkurl('','back')
      })
    }
    
  },

  openTimeHandle(e) { // 打开时间弹框
    const {item}=$.detail(e)
    if(this.data.submit=='1'){
      $.toast("已提交无法修改")
  }else{
    if(item=='addTime'){
      this.setData({
        isShowTime: true
      })
    }else{
      this.setData({
        isShowTime2: true
      })
    }
    
    }
  },
  timeConfirmHandle(e) { // 时间组件传出来的数据
    var { time, key } = e.detail
    this.setData({
      [key]: time
    })
    console.log(time, key)
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
 
})